package com.agranee.timesheet.bean;

import java.util.List;
import java.util.ListIterator;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

import com.agranee.timesheet.model.Country;
import com.agranee.timesheet.serviceable.CountryServiceable;

@ManagedBean(name = "country")
public class CountryBean {
	int id;
	private int countryCode;
	private String countryName;
	private String message;

	@ManagedProperty(value = "#{CountryService}")
	CountryServiceable countryService;

	public void addCountry() {
		Country country = new Country();
		country.setCountryName(this.getCountryName());
		country.setCountryCode(this.getCountryCode());
		getCountryService().addCountry(country);
	//	countryService.addCountry(country);
		this.message = "Country Added Successfully";
		clear();

	}

	public Country getCountryById(int id) {
		return getCountryService().getCountryById(id);
	}

	public List<Country> getAllCountryId() {
		return getCountryService().getAllCountryId();
	}

	public List<Country> getAllCountry() {
		return getCountryService().getAllCountry();
	}

	public void validateCountryCode(FacesContext context,
			UIComponent toValidate, Object value) throws ValidatorException {
		Integer integer = (Integer) value;
		int codeValue = integer.intValue();
		List<Country> list = getCountryService().getAllCountry();
		ListIterator<Country> listIterator = list.listIterator();
		while (listIterator.hasNext()) {
			if (listIterator.next().getCountryCode() == codeValue) {
				FacesMessage message = new FacesMessage(
						"Country code should be unique");
				throw new ValidatorException(message);

			}
		}
	}

	public void validateCountryName(FacesContext context,
			UIComponent toValidate, Object value) throws ValidatorException {
		String string = (String) value;
		List<Country> list = getCountryService().getAllCountry();
		ListIterator<Country> listIterator = list.listIterator();
		while (listIterator.hasNext()) {
			if (listIterator.next().getCountryName().equals(string)) {
				FacesMessage message = new FacesMessage(
						"Country Name should be unique");
				throw new ValidatorException(message);

			}
		}
	}

	public int getCountryCode() {
		return countryCode;
	}

	public void setCountryCode(int countryCode) {
		this.countryCode = countryCode;
	}

	public String getCountryName() {
		return countryName;
	}

	public void setCountryName(String countryName) {
		this.countryName = countryName;
	}

	public CountryServiceable getCountryService() {
		return countryService;
	}

	public void setCountryService(CountryServiceable countryService) {
		this.countryService = countryService;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void clear() {
		this.countryCode = 0;
		this.countryName = "";
	}

}
